Homework
Assignment 1
Due: 02/18/09
- Assume
that amount =11, m = 50, n =10 and p =5.
Evaluate the following expressions:
a) n
/ p + 3
b) m
/ p + n – 10 * amount
c) –p
* n
d) amount
/ 5
e) m
+ n / p + amount
- Write a
function that would be sent in and integer, determines if it is even or
odd and prints a corresponding even
or odd.
- For
the following code, determine the total number of items displayed. Also
determine the first and last numbers printed.
int num = 0;
while (num <= 20)
{
num++;
cout << num << “
“;
}
- What
is the output of the following loop?
for ( i = 20; i >= 0; i = i -4 )
cout << i
<< “ “;
- Use a
loop to display the multiples of 3 backwards from 33 to 3, inclusive.
- The following
declarations were used to cerate the prices
array:
const int NUMGRADES =
500;
double prices[ NUMGRADES ];
Write
the function header for a function named SortArray() that accepts the prices
array as a parameter named intAry and returns no value.
- Write
a small C++ function that takes an integer n and returns the sum of
all the integers smaller than n.
- What
is the error here?
char var1, ptr1;
var1 = ‘x’;
ptr1 = &var1;
- Assume
the definition
char c = ‘s’;
char d = ‘x’;
char e;
char *p1 = &c;
char *p2 = &d;
char *p3;
Assume further that
the address of c is 6940, the address of d is 9772 and the address of e is
2224. Now assume that these commands get executed: (the compilers
is really bad!!)
p1 = &c;
p2 = &d;
p3 = p1;
p1 = p2;
p2 = p3;
p3 = &e;
*p3 = *p1;
*p1 = *p2;
*p2 = *p3;
Give the values of the following
expressions:
a) &c
b) d
c) e
d) c
e) p2
f) p3
g) *p1
h) *p2
i)
*p3
- Write
a function to display the numbers in an array. The function receives the
size of the array and a pointer to the array. Display the values in the
array by changing the address of a pointer called dispPt.
- We
have two arrays, A and B, each containing 10 integers. Write a function
that checks if every element of array A is equal to its corresponding
element in array B. In other words, the function must check if A[0] is equal to B[0], A[1] is equal to B[1] and so on. The function must accept
only two pointer values and return an integer – zero for equal and not
zero for unequal.
- Give
the big-Oh characterization, in terms of n, of the running time of the
following code fragments:
- void
Ex1 ( int n ) {
int a;
for ( int i=0; i<n;
i++ )
a = i;
}
- void
Ex1 ( int n ) {
int a;
for ( int i=0; i<n;
i+=2 )
a = i;
}
- void
Ex1 ( int n ) {
int a;
for ( int i=0; i<n*n
i++ )
a = i;
}
- void
Ex1 ( int n ) {
int a;
for ( int i=0; i<n;
i++ )
for (
int j=0; j<=i; j++ )
a = i;
}
- If the
efficiency of the algorithm doIt can be expressed as O(n) =n2, calculate the efficiency of the following
program segment:
i = 1
loop ( i < n )
doIt( … )
i = i * 2
end loop
- Come
up with an algorithm of you choice that could
have two solutions. Show the two solutions and report on why you would use
one solution instead of the other in your program.